package me.xuym.crawler.czjyj;

import com.cbs.java.component.application.Application;
import com.cbs.java.component.application.BaseArgs;
import me.xuym.crawler.Processor;
import me.xuym.crawler.Task;
import me.xuym.crawler.simpleextension.dispatcher.SimpleDebugListDispatcher;
import me.xuym.crawler.simpleextension.dispatcher.SimpleEmailDispatcher;
import me.xuym.crawler.simpleextension.entity.SimpleLink;
import me.xuym.crawler.simpleextension.loader.SimpleWebPageLoader;
import me.xuym.crawler.simpleextension.processor.convertor.SimpleLinkList2EmailConvertor;
import me.xuym.crawler.simpleextension.processor.filter.SimpleBlackListFilter;
import me.xuym.crawler.simpleextension.processor.filter.SimpleCachedMySQLListFilter;
import me.xuym.crawler.simpleextension.processor.filter.SimpleWhiteListFilter;

import java.util.List;

/**
 * Created by broche on 08/09/2017.
 */
public class MyApplication extends Application {
    private BaseArgs baseArgs = new BaseArgs();

    public MyApplication(String[] args) {
        super(args);
    }

    @Override
    public boolean isDebug() {
        return false;
    }

    @Override
    public String applicationName() {
        return "crawler/czjyj";
    }

    @Override
    public BaseArgs getArgs() {
        return baseArgs;
    }

    @Override
    public void start() {
        super.start();
        try {

            Task task = new Task(this);
            Processor processor = task.load(new SimpleWebPageLoader(this))
                    .process(new CZJyjConvertor(this))
                    .process(new SimpleBlackListFilter(this))
                    .process(new SimpleWhiteListFilter(this))
                    .process(new SimpleCachedMySQLListFilter(this));

            // debug
            if (isDebug()) {
                processor.dispatch(new SimpleDebugListDispatcher(this));
            }

            // email
            processor.process(new SimpleLinkList2EmailConvertor(this) {
                @Override
                protected String subject(List<SimpleLink> simpleLinks) {
                    return String.format("【常州教育局】有%d条新消息", simpleLinks.size());
                }
            }).dispatch(new SimpleEmailDispatcher(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        MyApplication myApplication = new MyApplication(args);
        myApplication.start();
    }
}
